package cn.neusoft.baibang.service.impl;

import cn.neusoft.baibang.constants.RedisCourseConstant;
import cn.neusoft.baibang.domain.Product;
import cn.neusoft.baibang.domain.ProductDetails;
import cn.neusoft.baibang.domain.ProductType;
import cn.neusoft.baibang.dto.ProductManagementDto;
import cn.neusoft.baibang.exception.GlobaExceptionEnum;
import cn.neusoft.baibang.mapper.ProductMapper;
import cn.neusoft.baibang.service.IProductService;
import cn.neusoft.baibang.utils.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzm
 * @since 2022-12-10
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public void onLineProduct(Long productId) {
        // 参数校验
        AssertUtil.isNotNull(productId, GlobaExceptionEnum.COMMON_PARAM_IS_NULL);
        //业务校验
        Product product = selectById(productId);
        AssertUtil.isNotNull(product,GlobaExceptionEnum.NO_SUCH_DATA_PRODUCT);
        // 上架
        // 该表状态
        product.setState(Product.ONLINE);
        Date date = new Date();
        product.setUpperShelf(date);
        updateById(product);

        // 将上架的商品信息放到es中
        // 消息推送

    }

    @Override
    public void offLineProduct(Long productId) {
// 参数校验
        AssertUtil.isNotNull(productId, GlobaExceptionEnum.COMMON_PARAM_IS_NULL);
        //业务校验
        Product product = selectById(productId);
        AssertUtil.isNotNull(product,GlobaExceptionEnum.NO_SUCH_DATA_PRODUCT);

        Integer status = product.getState();
        boolean b = status == 1;
        AssertUtil.isTrue(b,GlobaExceptionEnum.THE_PRODUCT_HAS_BEEN_TAKEN_OFF_THE_SHELVES);
        product.setState(Product.OFFLINE);
        product.setLowerShelf(new Date());
        updateById(product);
    }

    @Override
    @Cacheable(cacheNames = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_NAMES,key = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_KEY)
    public List<Product> getPopularProducts() {

        Wrapper<Product> wrapper = new EntityWrapper<>();
        wrapper.orderBy("sales_olume",false);
        List<Product> products = selectList(wrapper);
        List<Product> list = new ArrayList<>();
        int a = 0;
        for (Product product : products) {
            list.add(product);
            ++a;
            if (a == 5){
                break;
            }
        }

        System.out.println(list);
        return list;
    }

    @Override
    public void add(ProductManagementDto dto) {
        Product product = dto.getProduct();
        ProductDetails productDetails = dto.getProductDetails();
        // 参数校验
        // 业务校验
        Product pro = productMapper.selectByProductName(product.getName());
        AssertUtil.isNull(pro, GlobaExceptionEnum.);
    }



    @Override
    @CacheEvict(cacheNames = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_NAMES,key = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_KEY)
    public boolean insert(Product entity) {
        return super.insert(entity);
    }
    @Override
    @CacheEvict(cacheNames = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_NAMES,key = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_KEY)
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }
    @Override
    @CacheEvict(cacheNames = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_NAMES,key = RedisCourseConstant.POPULAR_PRODUCTS_CACHE_KEY)
    public boolean updateById(Product entity) {
        return super.updateById(entity);
    }
}
